package com.visitor.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.visitor.mapper.UserMapper;
import com.visitor.pojo.DTO.UserRegisterDTO;
import com.visitor.pojo.entity.SysUser;

import com.visitor.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, SysUser> implements UserService {

    /**
     * 用户注册
     *
     * @param userRegisterDTO
     * @return
     */
    public boolean register(UserRegisterDTO userRegisterDTO) {
        //判断用户名是否存在
        boolean exist = lambdaQuery()
                .eq(SysUser::getUsername, userRegisterDTO.getUsername())
                .count() > 0;

        if(!exist){
            return false;
        }

        //用户名不存在，允许新建用户
        SysUser user = new SysUser();

        //save() 方法只能保存实体类（Entity），不能直接传 DTO
        BeanUtils.copyProperties(userRegisterDTO, user);

        save(user);
        return true;
    }
}